unit uOpenAL;

interface

uses
  SysUtils;

const
{$IFDEF WIN32}
  OpenALLib = 'openal32.dll';
{$ELSE}
  OpenALLib = 'libopenal.so';
{$ENDIF}


type
  TALboolean = Boolean;
  PALboolean = ^TALboolean;

  TALuByte = Char;
  PALuByte = PChar;

  TALuint = Cardinal;
  PALuint = ^TALuint;

  TALint = Integer;
  PALint = ^TALint;

  TALfloat = Single;
  PALfloat = ^TALfloat;

  TALsizei = Cardinal;
  PALsizei = ^TALsizei;
  
  TALenum = Integer;
  PALenum = ^TALenum;

  TALCenum = integer;
  PALCenum = ^TALCenum;
  
  TALCubyte = Char;
  PALCubyte = PChar;

  TALCuint = Cardinal;
  PALCuint = ^TALCuint;

  TALCint = integer;
  PALCint = ^TALCint;

  TALCvoid = Pointer;
  PALCvoid = ^TALCvoid;

  TALCdevice = TALCvoid;
  PALCdevice = ^TALCdevice;
  
  TALCcontext = TALCvoid;
  PALCcontext = ^TALCcontext;

const
  AL_PITCH        = $1003;
  AL_POSITION     = $1004;
  AL_LOOPING      = $1007;
  AL_BUFFER       = $1009;
  AL_GAIN         = $100A;
  AL_MIN_GAIN     = $100D;
  AL_SOURCE_STATE = $1010;
  AL_INITIAL      = $1011;
  AL_PLAYING      = $1012;
  AL_PAUSED       = $1013;
  AL_STOPPED      = $1014;

  AL_BUFFERS_QUEUED    = $1015;
  AL_BUFFERS_PROCESSED = $1016;

  AL_FORMAT_MONO8    = $1100;
  AL_FORMAT_MONO16   = $1101;
  AL_FORMAT_STEREO8  = $1102;
  AL_FORMAT_STEREO16 = $1103;

var
  alGenSources: procedure(n: TALsizei; sources: PALuint); cdecl;
  alDeleteSources: procedure(n: TALsizei; sources: PALuint); cdecl;
	
  alSourcei: procedure(source: TALuint; param: TALenum; value: TALint); cdecl;
  alSourcef: procedure(source: TALuint; param: TALenum; value: TALfloat); cdecl;
  alGetSourcei: procedure(source: TALuint; param: TALenum; value: PALint); cdecl;
  alSourcePlay: procedure(source: TALuint); cdecl;
  alSourcePause: procedure(source: TALuint); cdecl;
  alSourceStop: procedure(source: TALuint); cdecl;
  alSourceRewind: procedure(source: TALuint); cdecl;
	
  alGenBuffers: procedure(n: TALsizei; buffers: PALuint); cdecl;
  alDeleteBuffers: procedure(n: TALsizei; buffers: PALuint); cdecl;
  alBufferData: procedure(buffer: TALuint; format: TALenum; data: Pointer; size, freq: TALsizei); cdecl;
	
  alSourceQueueBuffers: procedure(source: TALuint; n: TALsizei; buffers: PALuint); cdecl;
  alSourceUnqueueBuffers: procedure(source: TALuint; n: TALsizei; buffers: PALuint); cdecl;
	
  alcCreateContext: function(device: TALCdevice; attrlist: PALCint): TALCcontext; cdecl;
  alcMakeContextCurrent: function(context: TALCcontext): TALCenum; cdecl;
  alcDestroyContext: procedure(context: TALCcontext); cdecl;
	
  alcOpenDevice: function(deviceName: PALCuByte): TALCdevice; cdecl;
  alcCloseDevice: procedure(device: TALCdevice); cdecl;

function InitOpenAL(libName: String = OpenALLib): Boolean;

implementation

uses
  uLib;

var
  lib: Cardinal = 0;

function InitOpenAL(libName: String): Boolean;
begin
  Result := False;

  lib := LibOpen(libName);

  if lib <> 0 then
  begin
    alGenSources := LibProc(lib, 'alGenSources');
    alDeleteSources := LibProc(lib, 'alDeleteSources');
    alSourcei := LibProc(lib, 'alSourcei');
    alSourcef := LibProc(lib, 'alSourcef');
    alGetSourcei := LibProc(lib, 'alGetSourcei');
    alSourcePlay := LibProc(lib, 'alSourcePlay');
    alSourcePause := LibProc(lib, 'alSourcePause');
    alSourceStop := LibProc(lib, 'alSourceStop');
    alSourceRewind := LibProc(lib, 'alSourceRewind');
    alGenBuffers := LibProc(lib, 'alGenBuffers');
    alDeleteBuffers := LibProc(lib, 'alDeleteBuffers');
    alBufferData := LibProc(lib, 'alBufferData');
    alSourceQueueBuffers := LibProc(lib, 'alSourceQueueBuffers');
    alSourceUnqueueBuffers := LibProc(lib, 'alSourceUnQueueBuffers');
    
    alcCreateContext := LibProc(lib, 'alcCreateContext');
    alcMakeContextCurrent := LibProc(lib, 'alcMakeContextCurrent');
    alcDestroyContext := LibProc(lib, 'alcDestroyContext');
    alcOpenDevice := LibProc(lib, 'alcOpenDevice');
    alcCloseDevice := LibProc(lib, 'alcCloseDevice');
    
    Result := True;
  end;
end;

end.
